1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect.testing.testers;
18
19 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20 import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
21 import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS;
22 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
23 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
24
25 import com.google.common.annotations.GwtCompatible;
26 import com.google.common.annotations.GwtIncompatible;
27 import com.google.common.collect.testing.AbstractCollectionTester;
28 import com.google.common.collect.testing.Helpers;
29 import com.google.common.collect.testing.features.CollectionFeature;
30 import com.google.common.collect.testing.features.CollectionSize;
31
32 import java.lang.reflect.Method;
33 import java.util.ConcurrentModificationException;
34 import java.util.Iterator;
35
36
37
38
39
40
41
42
43
44 @SuppressWarnings("unchecked")
45 @GwtCompatible(emulated = true)
46 public class CollectionAddTester<E> extends AbstractCollectionTester<E> {
47 @CollectionFeature.Require(SUPPORTS_ADD)
48 public void testAdd_supportedNotPresent() {
49 assertTrue("add(notPresent) should return true",
50 collection.add(samples.e3));
51 expectAdded(samples.e3);
52 }
53
54 @CollectionFeature.Require(absent = SUPPORTS_ADD)
55 public void testAdd_unsupportedNotPresent() {
56 try {
57 collection.add(samples.e3);
58 fail("add(notPresent) should throw");
59 } catch (UnsupportedOperationException expected) {
60 }
61 expectUnchanged();
62 expectMissing(samples.e3);
63 }
64
65 @CollectionFeature.Require(absent = SUPPORTS_ADD)
66 @CollectionSize.Require(absent = ZERO)
67 public void testAdd_unsupportedPresent() {
68 try {
69 assertFalse("add(present) should return false or throw",
70 collection.add(samples.e0));
71 } catch (UnsupportedOperationException tolerated) {
72 }
73 expectUnchanged();
74 }
75
76 @CollectionFeature.Require(
77 value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES},
78 absent = RESTRICTS_ELEMENTS)
79 public void testAdd_nullSupported() {
80 assertTrue("add(null) should return true", collection.add(null));
81 expectAdded((E) null);
82 }
83
84 @CollectionFeature.Require(value = SUPPORTS_ADD,
85 absent = ALLOWS_NULL_VALUES)
86 public void testAdd_nullUnsupported() {
87 try {
88 collection.add(null);
89 fail("add(null) should throw");
90 } catch (NullPointerException expected) {
91 }
92 expectUnchanged();
93 expectNullMissingWhenNullUnsupported(
94 "Should not contain null after unsupported add(null)");
95 }
96
97 @CollectionFeature.Require({SUPPORTS_ADD,
98 FAILS_FAST_ON_CONCURRENT_MODIFICATION})
99 @CollectionSize.Require(absent = ZERO)
100 public void testAddConcurrentWithIteration() {
101 try {
102 Iterator<E> iterator = collection.iterator();
103 assertTrue(collection.add(samples.e3));
104 iterator.next();
105 fail("Expected ConcurrentModificationException");
106 } catch (ConcurrentModificationException expected) {
107
108 }
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122
123 @GwtIncompatible("reflection")
124 public static Method getAddNullSupportedMethod() {
125 return Helpers.getMethod(CollectionAddTester.class, "testAdd_nullSupported");
126 }
127
128
129
130
131
132
133
134
135 @GwtIncompatible("reflection")
136 public static Method getAddNullUnsupportedMethod() {
137 return Helpers.getMethod(CollectionAddTester.class, "testAdd_nullUnsupported");
138 }
139
140
141
142
143
144
145
146
147 @GwtIncompatible("reflection")
148 public static Method getAddUnsupportedNotPresentMethod() {
149 return Helpers.getMethod(CollectionAddTester.class, "testAdd_unsupportedNotPresent");
150 }
151 }